package com.software.shipperagent.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.software.common.CommonUtils;
import com.software.form.billform.dao.BillForm;
import com.software.form.billform.dao.BillFormTransaction;
import com.software.form.bookingform.dao.BookingFormTransaction;
import com.software.form.cargobooking.dao.CargoBooking;
import com.software.form.cargobooking.dao.CargoBookingTransaction;
import com.software.form.inquiry.sagent.dao.InquirySagentTransaction;
import com.software.form.inquiryform.dao.InquiryForm;
import com.software.form.inquiryform.dao.InquiryFormTransaction;

public class BillForwardServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		BillFormTransaction bft = (BillFormTransaction)CommonUtils.getBean("billFormTransaction");
		String email = (String) request.getSession().getAttribute("user");
		List<BillForm> bills = bft.getBillsBySagentEmail(email);
		List list = new ArrayList();
		if(bills != null){
			for(int i = 0; i < bills.size(); i++){
				JSONArray array = new JSONArray();
				array.put(bills.get(i).getNo());
				array.put(bills.get(i).getCompany());
				array.put(bills.get(i).getConsigneeName());
				array.put(bills.get(i).getConsigneeAddress());
				array.put(bills.get(i).getDepositBank());
				array.put(bills.get(i).getAccount());
				array.put(bills.get(i).getCargoName());
				list.add(array);
			}
			JSONObject o = new JSONObject();
			try {
				o.put("bills", list);
				CommonUtils.outputJsonData(o.toString(), response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		JSONObject json = CommonUtils.readJSONStringFromRequestBody(request, response);
		BillFormTransaction bft = (BillFormTransaction)CommonUtils.getBean("billFormTransaction");
		try{
			String no = json.getString("no");
			String shipperEmail = json.getString("shipperEmail");
			if(bft.updateShipperEmail(no, shipperEmail))
				CommonUtils.outputJsonData(1, response);
			else
				CommonUtils.outputJsonData(0, response);
		}catch(JSONException je){
			try {
				CommonUtils.outputJsonData(0, response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	
}
